﻿using System;
using System.Drawing;
using System.Windows.Forms;
using TEL.ClientClasses;
using TEL.TelPhoneF.InfoList;

namespace TEL.TelPhoneF
{
    public partial class FrmHistoryCall : Form
    {
        private void CreateRegion()
        {
            Paths p = new Paths();
            this.Region = new Region(p.GetHistoryPath(new Point(0, 0), this.Size));
          
        }
        public FrmHistoryCall()
        {
            InitializeComponent();
            CreateRegion();
        }

        public void OnMissingViewChanged()
        {
            dataListHistory.OnMissingViewChanged();
        }
        public void AddCallHistory(ContactCall contactCall)
        {
            dataListHistory.AddCallHistory(contactCall);
            if(Vid)
            {
                dataListHistory.MissingView();

            }
        }

        public void DeleteMissing(string phone)
        {
            if(!string.IsNullOrEmpty(phone)) dataListHistory.DeleteMissing(phone);
        }

      //  public string m_SearchContact = string.Empty;

        public Contact FindFirstContact(string searchKey)
        {
            try
            {

                if (string.IsNullOrEmpty(searchKey)) return null;

                return dataListHistory.FindFirstContact(searchKey);
            }
            catch (Exception error)
            {
                if (ErrorEvent != null) ErrorEvent(this, new StrErrorEventArgs("FrmHistoryCall FindFirstContact", error, false));
            }

            return null;
        }

        public string FindNameByPhone(string phone)
        {
            try
            {
                if (string.IsNullOrEmpty(phone)) return string.Empty;
                return dataListHistory.FindNameByPhone(phone);
            }
            catch (Exception error)
            {
                if (ErrorEvent != null) ErrorEvent(this, new StrErrorEventArgs("FrmHistoryCall FindNameByPhone  -  " + phone, error, false));
            }

            return null;


        }

        public void UpdateStateHistory(DateTime dateTime, ContactCallState state)
        {
            dataListHistory.UpdateStateHistory(dateTime, state);
        }

        public event EventHandler<StringEventArgs> HistoryCall;

        private void dataListHistory_HistoryCall(object sender, StringEventArgs e)
        {
            if (HistoryCall != null) HistoryCall(this, e);
        }

        private bool m_vid = false;
        public bool Vid
        {
            get { return m_vid; }
            set
            {
                try
                {
                    m_vid = value;
                    if (!value) this.Hide();
                    else
                    {
                        this.Show();
                      //  this.WindowState = FormWindowState.Normal;
                      //  this.Activate();
                    }
                    dataListHistory.Vid = value;
                }
                catch (Exception error)
                {
                    if (ErrorEvent != null) ErrorEvent(this, new StrErrorEventArgs("FrmHistoryCall VID", error, false));
                }
            }
        }

        private void FrmHistoryCall_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            Vid = false;
        }
         public event EventHandler<StrErrorEventArgs> ErrorEvent;
        private void dataListHistory_ErrorEvent(object sender, StrErrorEventArgs e)
        {
            if (ErrorEvent != null) ErrorEvent(sender, e);
        }

        public event EventHandler<BoolEventArgs> MissingViewChanged;

        private void dataListHistory_MissingViewChanged(object sender, BoolEventArgs e)
        {
            if (MissingViewChanged != null) MissingViewChanged(sender, e);
        }

        private void FrmHistoryCall_Activated(object sender, EventArgs e)
        {
            try
            {
                dataListHistory.MissingView();
            }
            catch (Exception error)
            {
            //    if (ErrorEvent != null) ErrorEvent(this, new StrErrorEventArgs("FrmHistoryCall Activated", error, false));
            }
        }

        public void SetFilterOut()
        {
            dataListHistory.btnFilterOut.PerformClick();
        }

        private void dataListHistory_WidthChanged(object sender, EventArgs e)
        {
            this.Size = new Size( dataListHistory.HistoryWidth(), this.Size.Height) ;
        }

        private void dataListHistory_SizeChanged(object sender, EventArgs e)
        {
            CreateRegion();
        }
    }
}
